package com.itheima.springbootwebtlias.controller;


import com.itheima.springbootwebtlias.pojo.PageResult;
import com.itheima.springbootwebtlias.pojo.Result;
import com.itheima.springbootwebtlias.pojo.Student;
import com.itheima.springbootwebtlias.pojo.StudentQueryParam;
import com.itheima.springbootwebtlias.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/students")
@RestController
public class StudentController{

    @Autowired
    private StudentService studentService;

    // 条件分页查询接口
    @GetMapping
    public Result page(StudentQueryParam studentQueryParam) {
        log.info("分页查询，参数：{}", studentQueryParam);
        // 调用业务层方法
        PageResult pageResult = studentService.page(studentQueryParam);
        return Result.success(pageResult);
    }

    // 添加学员
    @PostMapping
    public Result save(@RequestBody Student student) {
        log.info("添加学员，参数：{}", student);
        studentService.insert(student);
        return Result.success();
    }

    // 根据id查询学员接口
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据ID查询学员，id：{}", id);
        Student student = studentService.getById(id);
        return Result.success(student);
    }

    // 根据id查询到之后修改学员信息
    @PutMapping
    public Result update(@RequestBody Student student) {
        log.info("修改学员信息，参数：{}", student);
        studentService.update(student);
        return Result.success();
    }

    // 批量删除学员信息
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("批量删除学员信息，ids：{}", ids);
        studentService.deleteByIds(ids);
        return Result.success();
    }

    // 违纪处理接口
    @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable Integer id, @PathVariable Integer score) {
        log.info("违纪处理，id：{}，score：{}", id, score);
        if(score <= 0){
            return Result.error("扣分不能小于等于0");
        }
        studentService.updateViolation(id, score);
        return Result.success();
    }
}
